# %%
# -------------------图像通道分离，合并，灰度，彩色转换-------------------
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# %%
img = cv.imread('test.jpg')
cv.imshow('Original', img)
cv.waitKey(0)
cv.destroyAllWindows()
plt.imshow(img)

# %%
# 通道分离
b, g, r = cv.split(img)  # 分别获取蓝色、绿色、红色通道
cv.imshow('Blue', b)
cv.imshow('Green', g)
cv.imshow('Red', r)
cv.waitKey(0)
cv.destroyAllWindows()
plt.imshow(b)
plt.imshow(g)
plt.imshow(r)

# %%
# 合并通道
merged = cv.merge([b, g, r])
cv.imshow('Merged', merged)
cv.waitKey(0)
cv.destroyAllWindows()
plt.imshow(merged)

# %% 图像转换
# 灰度转换
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
cv.waitKey(0)
# 彩色转换HSV， Hue (色相)、Saturation (饱和度) 和 Value (明度)
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
cv.imshow('HSV', hsv)
cv.waitKey(0)
plt.imshow(hsv)

# %%
cv.destroyAllWindows()
 